/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package l1j.server.server.clientpackets;


import l1j.server.server.ClientThread;
import l1j.server.server.model.L1World;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_PacketBox;
import l1j.server.server.serverpackets.S_SystemMessage;
import l1j.server.server.serverpackets.S_WhoAmount;
import l1j.server.server.serverpackets.S_WhoCharinfo;
import l1j.server.server.utils.Random;

import l1j.server.Config;

import l1j.taki.GetNowTime;
import l1j.taki.L1GameReStart;

// Referenced classes of package l1j.server.server.clientpackets:
// ClientBasePacket

/**
 * 處理收到由客戶端傳來查詢線上人數的封包
 */
public class C_Who extends ClientBasePacket {

	private static final String C_WHO = "[C] C_Who";

	public C_Who(byte[] decrypt, ClientThread client) {
		super(decrypt);
		String s = readS();
		L1PcInstance find = L1World.getInstance().getPlayer(s);
		L1PcInstance pc = client.getActiveChar();

		if (find != null) {
			S_WhoCharinfo s_whocharinfo = new S_WhoCharinfo(find);
			pc.sendPackets(s_whocharinfo);
		} else { // who 顯示start by 0724
			String amount = String.valueOf(L1World.getInstance().getAllPlayers().size());			
			// 刪除S_WhoAmount s_whoamount = new S_WhoAmount(amount);
			// 刪除pc.sendPackets(s_whoamount);
			// 線上資訊
			if (pc.getAccessLevel() == 200) {
				pc.sendPackets(new S_PacketBox(S_PacketBox.CALL_SOMETHING));
			}
			int chance = Random.nextInt(2) + 1;
			if (chance <= 1 ){
				amount = String.valueOf(L1World.getInstance().getAllPlayers().size()*Config.O_P_D + Config.O_P);
			} else if (chance > 1){
				amount = String.valueOf(L1World.getInstance().getAllPlayers().size()*Config.O_P_D + Config.O_P+1);
			}			
			S_WhoAmount s_whoamount = new S_WhoAmount(amount);
			pc.sendPackets(s_whoamount);
			// 今天日期
			int Mon = GetNowTime.GetNowMonth();// 月份錯誤補正
			pc.sendPackets(new S_SystemMessage("\\fU" + "今天是 "+ GetNowTime.GetNowYear() + " 年 " + (Mon + 1)+ " 月 " + GetNowTime.GetNowDay() + " 日。"));
			// 目前時間
			pc.sendPackets(new S_SystemMessage("\\fU" + "現在時間(24h): "+ GetNowTime.GetNowHour() + " 時 "+ GetNowTime.GetNowMinute() + " 分 "+ GetNowTime.GetNowSecond() + " 秒。"));
			int second = L1GameReStart.getInstance().GetRemnant();
			pc.sendPackets(new S_SystemMessage("\\fU" + "距離伺服器重啟時間還有: "+ (second / 60) / 60 + " 小時 " + (second / 60) % 60+ " 分 " + second % 60 + " 秒。"));
			// who 顯示end by 0724
		}
	}

	@Override
	public String getType() {
		return C_WHO;
	}
}


